package com.llyb.config;

import com.llyb.service.ChatAssistant;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class LLMConfig {

    @Bean
    public ChatModel chatModel(){
        return OpenAiChatModel.builder().
                apiKey(System.getenv("aliQwen-api")).
                modelName("qwen-plus").
                baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1").
                build();
    }


    @Bean
    public InMemoryEmbeddingStore<TextSegment> inMemoryEmbeddingStore(){

        return new InMemoryEmbeddingStore<>();
    }


    @Bean
    public ChatAssistant assistant(ChatModel chatModel, EmbeddingStore<TextSegment> embeddingStore){
        return AiServices.builder(ChatAssistant.class).
                chatModel(chatModel).
                chatMemory(MessageWindowChatMemory.withMaxMessages(50)).
                contentRetriever(EmbeddingStoreContentRetriever.from(embeddingStore)).
                build();
    }

}